﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace QuanLySimSo
{
    class clsSim // Class dinh nghia doi tuong Sim. Cac ban khong duoc chinh sua trong nay.
    {
        string ChuoiKetNoi = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\sqlQLSim.mdf;Integrated Security=True;User Instance=True";
        //string ChuoiKetNoi = @"Server=.\SQLEXPRESS;Database=sqlQLSim1;Integrated Security=SSPI;Trusted_Connection=Yes;";

        public string m_strSoSim;
        public string m_strDauSo;
        public double m_fGia;
        public string m_strLoai;
        public string m_strDaBan;

        public clsSim()     //khởi tạo Sim mặc định
        {
            m_strSoSim = "";
            m_strDauSo = "";
            m_fGia = 0;
            m_strLoai = "11";
            m_strDaBan = "CHUA";
        }

        public clsSim(string strSoSim, string strDauSo, double fGia, string strLoai, string strDaBan) // khởi tạo Sim có tham số
        {
            m_strSoSim = strSoSim;
            m_strDauSo = strDauSo;
            m_fGia = fGia;
            m_strLoai = strLoai;
            m_strDaBan = strDaBan;
        }

        public string SOSIM
        {
            get { return m_strSoSim; }
            set { this.m_strSoSim = value; }
        }

        public string DAUSO
        {
            get { return m_strDauSo; }
            set { this.m_strDauSo = value; }
        }

        public double GIA
        {
            get { return m_fGia; }
            set { this.m_fGia = value; }
        }

        public string LOAI
        {
            get { return m_strLoai; }
            set { this.m_strLoai = value; }
        }

        public string DABAN
        {
            get { return m_strDaBan; }
            set { this.m_strDaBan = value; }
        }

        public int AddSim()     //Them mot Sim moi vao CSDL
        {
            SqlConnection Connect = new SqlConnection(ChuoiKetNoi);
            Connect.Open();
            try
            {
                string insert = "insert into Sim(SoSim,DauSo,Gia,LoaiSim,DaBan) values('" + m_strSoSim + "','" + m_strDauSo + "'," + m_fGia + ",'" + m_strLoai + "','" + m_strDaBan + "')";
                SqlCommand mySqlCommand = new SqlCommand(insert, Connect);
                mySqlCommand.ExecuteNonQuery();

                Connect.Close();

                return 1; // thêm Sim thành công

            }
            catch
            {
                Connect.Close();
                return 0; // trùng Sim
            }
        }

        public int EditSim()    //Sua thong tin cua Sim trong CSDL
        {
            SqlConnection mySqlConnection = new SqlConnection(ChuoiKetNoi);
            SqlCommand mySqlCommand = new SqlCommand("update Sim set Gia=" + m_fGia + ", daban='" + m_strDaBan + "' where sosim ='" + m_strSoSim + "'", mySqlConnection);
            if (mySqlConnection.State == ConnectionState.Closed)
                mySqlConnection.Open();
            try
            {
                mySqlCommand.ExecuteNonQuery();
                if (mySqlConnection.State == ConnectionState.Open)
                    mySqlConnection.Close();
                return 1;
            }
            catch
            {
                mySqlConnection.Close();
                return 0;
            }
        }

        public int RemoveSim()  //Xoa mot Sim torng CSDL
        {
            SqlConnection mySqlConnection = new SqlConnection(ChuoiKetNoi);
            SqlCommand mySqlCommand = new SqlCommand("delete from Sim where SoSim='" + m_strSoSim + "'", mySqlConnection);
            //string select = "delete from Sim where SoSim='" + m_strSoSim + "'";
            if (mySqlConnection.State == ConnectionState.Closed)
                mySqlConnection.Open();
            try
            {
                mySqlCommand.ExecuteNonQuery();
                //SqlDataAdapter da = new SqlDataAdapter(select, mySqlConnection);
                if (mySqlConnection.State == ConnectionState.Open)
                    mySqlConnection.Close();
                return 1;
            }
            catch
            {
                mySqlConnection.Close();
                return 0;
            }
        }

        public DataTable LayTTSim() // Lấy thông tin Sim đưa vào DataTable để gán vào DataGridView
        {
            SqlConnection Connect = new SqlConnection(ChuoiKetNoi);
            Connect.Open();
            DataTable dt = new DataTable();
            try
            {
                string select = "select SoSim,Gia,DaBan,Mang,LoaiSim from Sim,Mang where Sim.Dauso = mang.dauso";
                SqlDataAdapter da = new SqlDataAdapter(select, Connect);

                da.Fill(dt);
                Connect.Close();

                return dt; // lấy thành công
            }
            catch
            {
                Connect.Close();
                return dt; // lấy không thành công
            }
        }
    }
}
